package com.egao.common.manage.big.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.manage.big.vo.BigDataJzppCollegeVO;
import com.egao.common.module.pushInformation.PushInformationConstants;
import com.egao.common.module.pushInformation.entity.PushInformation;
import com.egao.common.module.pushInformation.entity.PushInformationPosition;
import com.egao.common.module.pushInformation.param.PushInformationParam;
import com.egao.common.module.pushInformation.service.PushInformationPositionService;
import com.egao.common.module.pushInformation.service.PushInformationService;
import com.egao.common.module.system.entity.College;
import com.egao.common.module.system.entity.PushViewRecord;
import com.egao.common.module.system.service.CollegeService;
import com.egao.common.module.system.service.PushViewRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "大屏精准匹配数据源")
@RestController
@RequestMapping("${api}")
public class BigDataJzppController extends BaseController {

    @Resource
    private PushInformationService pushInformationService;
    @Resource
    private CollegeService collegeService;
    @Resource
    private PushViewRecordService pushViewRecordService;
    @Resource
    private PushInformationPositionService pushInformationPositionService;

    @PreAuthorize("hasAuthority('big:data')")
    @ApiOperation(value = "学院精准匹配")
    @GetMapping("/big/data/jzpp/college")
    public JsonResult college(Integer year, Integer degree) {
        // 精准匹配数据源
        List<PushInformation> list = pushInformationService.list(new LambdaQueryWrapper<PushInformation>()
                .eq(PushInformation::getYearId, year)
                .eq(PushInformation::getPushState, PushInformationConstants.PUSH_STATE_SUCCESS)
        );

        // 学院数据源
        List<College> collegeList = collegeService.listAll(degree, "");

        // 辅导员查看数据源
        List<PushViewRecord> recordList = pushViewRecordService.list();

        // 结果数据源
        List<BigDataJzppCollegeVO> voList = new ArrayList<>();

        for (College college : collegeList) {
            int count = 0;
            int readCount = 0;
            int resumeCount = 0;
            int collegeCount = 0;

            for (PushInformation pushInformation : list) {
                if (!college.getName().equals(pushInformation.getCollegeName())) {
                    continue;
                }

                count++;
                readCount += PushInformationConstants.READ_STATE_READ == pushInformation.getReadState()
                        ? 1 : 0;
                resumeCount += PushInformationConstants.RESUME_STATE_SEND == pushInformation.getResumeState()
                        ? 1 : 0;
                collegeCount += (int) recordList.stream().filter(d -> pushInformation.getId().equals(d.getPushInformationId())).count();
            }

            BigDataJzppCollegeVO vo = new BigDataJzppCollegeVO();
            vo.setCollegeName(college.getName());
            vo.setData(new int[] {count, readCount, resumeCount, collegeCount});
            voList.add(vo);
        }

        return JsonResult.ok().setData(voList);
    }

    @PreAuthorize("hasAuthority('big:data')")
    @ApiOperation(value = "学院精准匹配明细")
    @GetMapping("/big/data/jzpp/college/detail")
    public PageResult<PushInformation> list(HttpServletRequest request) {
        // 查询已推送信息
        PageParam<PushInformationParam> pageParam = new PageParam<>(request);
        pageParam.getPageData().put("pushState", PushInformationConstants.PUSH_STATE_SUCCESS);
        List<PushInformation> list = pushInformationService.listPage(pageParam);
        if (CollectionUtil.isEmpty(list)) {
            return new PageResult<>(new ArrayList<>());
        }

        // 根据当前所有推送信息ID查询所有关联推送职位
        List<Integer> pushInformationIds = list.stream().map(PushInformation::getId).collect(Collectors.toList());
        List<PushInformationPosition> pushInformationPositionList = pushInformationPositionService.list(
                new LambdaQueryWrapper<PushInformationPosition>()
                        .in(PushInformationPosition::getPushInformationId, pushInformationIds)
        );

        for (PushInformation pushInformation : list) {
            pushInformation.setPositionList(pushInformationPositionList.stream().
                    filter(d -> d.getPushInformationId().equals(pushInformation.getId())).collect(Collectors.toList())
            );

            String categoryLabel = "";
            if (1 == pushInformation.getCategory()) {
                categoryLabel = "招聘信息";
            }
            if (2 == pushInformation.getCategory()) {
                categoryLabel = "宣讲会";
            }
            if (3 == pushInformation.getCategory()) {
                categoryLabel = "空中宣讲会";
            }
            pushInformation.setCategoryLabel(categoryLabel);

            pushInformation.setReadStateLabel(pushInformation.getReadState() == 0 ? "已查看" : "未查看");
            pushInformation.setResumeStateLabel(pushInformation.getResumeState() == 0 ? "已发送" : "未发送");

            StringBuilder jobs = new StringBuilder();
            for (PushInformationPosition pushInformationPosition : pushInformationPositionList) {
                if (pushInformation.getId().equals(pushInformationPosition.getPushInformationId())) {
                    String job = StrUtil.format("{}({})",
                            pushInformationPosition.getPositionName(),
                            pushInformationPosition.getMatching()
                    );
                    jobs.append(job).append(",");
                }
            }
            pushInformation.setJobs(StrUtil.removeSuffix(jobs.toString(), ","));
        }

        return new PageResult<>(list, pageParam.getTotal());
    }
}
